﻿package com.library.utils
{
	public class StringUtil
	{
		public static const MONTH_NAMES:Array = ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"];
		/**
		* format the millisecond as the NN:NN:NN string
		* contain the Hour
		* @param	millisecond
		* @return a formatted string
		*/
		public static function FormatHour(millisecond:Number):String
		{
			var hourStr:String;
			var minuteStr:String;
			var secondStr:String;
			var second:Number=Math.floor(millisecond/1000);
			secondStr=MergeString("00",String(second%60));
			second=Math.floor(second/60);
			minuteStr=MergeString("00",String(second%60));
			second=Math.floor(second/60);
			hourStr=MergeString("00",String(second%60));
			second=Math.floor(second/60);
			return hourStr+":"+minuteStr+":"+secondStr;
		}
		
		/**
		* format the second as the NN:NN string
		* contain the Minute
		* @param	second
		* @return a formatted string
		*/
		public static function FormatMinute(second:uint):String
		{
			if(second>=3600)
			{
				return FormatHour(second*1000);
			}
			var minuteStr:String
			var secondStr:String;
			secondStr=MergeString("00",String(second%60));
			second=Math.floor(second/60);
			minuteStr=MergeString("00",String(second%60));
			return minuteStr+":"+secondStr;
		}
		
		// 把后面的字符串贴到目标串尾部，替换原有位置的字符。
		private static function MergeString(aim:String,source:String):String
		{
			var resultStr:String;
			if(source.length >= aim.length)
			{
				resultStr=source;
			}else
			{
				resultStr=aim;
				resultStr=resultStr.slice(0,(aim.length-source.length));
				resultStr=resultStr+source;
			}
			return resultStr;
		}
		
		/**
		 * replace the variable in the string "{$variable}" as a widly used strategy
		 * 
		 * @return string after replacing
		 */ 
		public static function ReplaceVariables(str:String,sourceObj:Object,escapeVars:Boolean=false):String
		{
			var resultStr:String=str;
			for(var i:String in sourceObj)
			{
				var patternStr:String="\\{"+i+"\\}";
				var pattern:RegExp=new RegExp(patternStr,"g");
				resultStr=resultStr.replace(pattern,String(sourceObj[i]))
			}
			if(escapeVars)
			{
				return escape(resultStr);
			}
			return resultStr;
		}
		
		public static function ValidEmail(emailAdress:String):Boolean
		{
			var emailRegExp:RegExp = /\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i;
			return emailRegExp.test(emailAdress);
		}
		
		public static function SliceString(str:String, length:int, showSuspension:Boolean = true):String{
			var result:String;
			if(length < str.length){
				if(showSuspension){
					result = str.substr(0, length-3);
					result += "...";	
				}else{
					result = str.substr(0, length);
				}
			}else{
				return result = str;
			}
			return result;
		}
		
		public static function GetFormatData(date:Date):String{
			var M:Number = date.getMonth();
			var month:String = MONTH_NAMES[M-1];
			var day:String = date.getDate().toString();
			var year:String = date.getFullYear().toString();
			
			var result:String = month + " " + day + "," + year.substr(year.length-2, year.length); 
			return result;
		}
		
		// 输入 7，得到如  007 这样的返回值
		public static function GetStringNumber(number:int, length:int):String{
			var space:int = length - number.toString().length;
			if(space < length){
				var result:String = "";
				for(var i:uint = 0; i < space; i++){
					result += "0";
				}
				result += number;
				return result;
			}else{
				return number.toString();
			}
		}
		
		public static function Trim(value:String):String  {
			return value.replace(/(^\s*)|(\s*$)/g, "");
		}
		
	}
}
